package com.chushouya.manager.dao.repository;

import com.chushouya.manager.dao.entity.MerchantUserEntity;
import com.chushouya.manager.dao.mapper.MerchantUserEntityMapper;
import com.chushouya.manager.dto.admin.merchant.MerchantUserQuery;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;

@Repository
public class MerchantUserRepository extends BaseRepository<MerchantUserEntityMapper, MerchantUserEntity, Long> {

    public MerchantUserEntity selectOne(MerchantUserQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    private Example buildExample(MerchantUserQuery query) {
        Example example = this.buildExample();
        if (Objects.nonNull(query.getMerchantId())) {
            example.and().andEqualTo("merchantId", query.getMerchantId());
        }
        if (Objects.nonNull(query.getUserId())) {
            example.and().andEqualTo("userId", query.getUserId());
        }
        if (Objects.nonNull(query.getStatus())) {
            example.and().andEqualTo("status", query.getStatus());
        }
        if (Strings.isNotEmpty(query.getUserPhone())) {
            example.and().andEqualTo("userPhone", query.getUserPhone());
        }
        return example;
    }

    public PageResult<MerchantUserEntity> selectPageResult(MerchantUserQuery query) {
        return this.selectPage(query.getPageRequest(), this.buildExample(query));
    }
}
